<%-- 
    Document   : SaveData
    Created on : 16 Apr 12, 15:17:14
    Author     : hartono
--%>

<%@page import="main.LogicImageSegmentation"%>
<jsp:useBean id="lis" class="main.LogicImageSegmentation" scope="application"/>
<!DOCTYPE html>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="org.fivekage.DBHelper"%>

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>

<%!
    public static String getExtension(String file) {
            int dotIndex = file.lastIndexOf(".");
            String extension = file.substring(dotIndex + 1,file.length());
            return extension;        
    }

    public static String getFileName(String file) {
        
            int dotIndex = file.lastIndexOf(".");
            int fileSeparatorIndex = file.lastIndexOf(File.separator);
            String fileName = file.substring(fileSeparatorIndex + 1,
                    dotIndex);
            return fileName;       

    }

%>

<%
    // Database connection starting ...   
    try {
        // get the app path
        String appPath = config.getServletContext().getRealPath("/");
        //System.out.println("APP PATH : "+appPath);
        // data set path                 
        File ff = new File(appPath);
        //String dataSetPath = ff.getParentFile().getParentFile().toString() + "/";     // get /ImageSegmentation/ ..
        String dataSetPath = appPath;   // get /ImageSegmentation/ ..
        File fff = new File(dataSetPath);

        // checking whether form is multipart or not
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (!isMultipart) {
            // no multipart                
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();                
            }

            Iterator itr = items.iterator();
            String fileName = "";
            // iterate the form element submitted
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                    // not used here ... to get form element 
                    // item = form field
                } else {
                    // item is not a form field , but it is a file uploaded to server
                    // item name -> file name -> item.getName()                
                    // make directory for saving data set
                    File dirImage = new File(dataSetPath+"/src/java/data_set/");
                    //File dirImage = new File(dataSetPath + "/src/java/");

                    if (!dirImage.exists()) {
                        // if dont exists , then make it                         
                        dirImage.mkdirs();
                    }else{
                                               
                    }

                    // make new item in the data set path                    
                    if (getExtension(item.getName()).equalsIgnoreCase("arff")) {
                        File file = new File(dataSetPath+ LogicImageSegmentation.FILE_DATA_TEST_NAME_ARFF);                        
                        File csvFile = new File(dataSetPath+ LogicImageSegmentation.FILE_DATA_TEST_NAME_CSV);                        
                        
                        item.write(file); // write the content of file -> save it..fufufufu                                                                    
                        try{
                         LogicImageSegmentation.SaveArffToCSV(file.toString(),csvFile.toString());                         
                        out.println("<script type='text/javascript'>");
                        out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>SUCCESSFULLY UPLOADED</center>\";");
                        out.println("</script>");                        
                        }
                        catch(Exception e){
                            System.out.println("DEBUG");
                            e.printStackTrace();
                            out.println("<script type='text/javascript'>");
                            out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>Error di csv</center>\";");
                            out.println("</script>"); 
                        }
                        lis.InisializesInstances(dataSetPath + LogicImageSegmentation.FILE_DATA_TEST_NAME_ARFF, LogicImageSegmentation.ARFF_FILE);
                    } else if (getExtension(item.getName()).equalsIgnoreCase("csv")) {
                        File file = new File(dataSetPath+ LogicImageSegmentation.FILE_DATA_TEST_NAME_CSV);                        
                        item.write(file); // write the content of file -> save it..fufufufu                                            
                        File arffFile= new File(dataSetPath+ LogicImageSegmentation.FILE_DATA_TEST_NAME_ARFF);
                                                
                        try{
                         LogicImageSegmentation.SaveCSVToArff(file.toString(),arffFile.toString());
                         out.println("<script type='text/javascript'>");
                        out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>SUCCESSFULLY UPLOADED</center>\";");
                        out.println("</script>");
                          }
                        catch(Exception e){
                            System.out.println("DEBUG");
                            e.printStackTrace();
                            out.println("<script type='text/javascript'>");
                            out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>Error di arff</center>\";");
                            out.println("</script>"); 
                        }
                        
                        lis.InisializesInstances(dataSetPath + LogicImageSegmentation.FILE_DATA_TEST_NAME_CSV, LogicImageSegmentation.ARFF_FILE);                        
                    } else {                        
                        out.println("<script type='text/javascript'>");
                        out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>Please check extension</center>\"; ");
                        out.println("</script>");
                    }
                    //System.out.println(lis.m_Instances);
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Exception Error");
        e.printStackTrace();
        // database connection failed        
        out.println("<script type='text/javascript'>");
        out.println("parent.document.getElementById('statusdatatest').innerHTML=\"<center>ERROR</center>\";");
        out.println("</script>");
    }
%>
